#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 2e5 + 5;

mt19937 mt(chrono::steady_clock::now().time_since_epoch().count());
int n, m;
int a[N], b[N], v[N], t[N];
ll mx[N << 2], tag[N << 2];

void addtag(ll x, int p) {
  tag[p] += x;
  mx[p] += x;
}
void pushup(int p) { mx[p] = max(mx[p << 1], mx[p << 1 | 1]); }
void pushdown(int p) {
  if (tag[p]) {
    addtag(tag[p], p << 1);
    addtag(tag[p], p << 1 | 1);
    tag[p] = 0;
  }
}
void build(int p, int l, int r) {
  mx[p] = tag[p] = 0;
  if (l == r) return;
  int mid = (l + r) >> 1;
  build(p << 1, l, mid);
  build(p << 1 | 1, mid + 1, r);
}
void update(int L, int R, ll x, int p, int l, int r) {
  if (L <= l && R >= r) {
    addtag(x, p);
    return;
  }
  pushdown(p);
  int mid = (l + r) >> 1;
  if (L <= mid) update(L, R, x, p << 1, l, mid);
  if (R > mid) update(L, R, x, p << 1 | 1, mid + 1, r);
  pushup(p);
}
void update(int x, ll v, int p, int l, int r) {
  if (l == r) {
    mx[p] = v;
    return;
  }
  pushdown(p);
  int mid = (l + r) >> 1;
  if (x <= mid)
    update(x, v, p << 1, l, mid);
  else
    update(x, v, p << 1 | 1, mid + 1, r);
  pushup(p);
}
ll query(int L, int R, int p, int l, int r) {
  if (L <= l && R >= r) return mx[p];
  pushdown(p);
  int mid = (l + r) >> 1;
  ll res = -1e18;
  if (L <= mid) res = max(res, query(L, R, p << 1, l, mid));
  if (R > mid) res = max(res, query(L, R, p << 1 | 1, mid + 1, r));
  return res;
}
void solve() {
  cin >> n;
  rep(i, 1, n) cin >> v[i];
  rep(i, 1, n) cin >> a[i];
  rep(i, 1, n) cin >> b[i];
  rep(i, 1, n) t[b[i]] = i;
  rep(i, 1, n) a[i] = t[a[i]];
  rep(i, 1, n) t[i] = v[b[i]];
  build(1, 0, n);
  rep(i, 1, n) {
    ll x = t[a[i]];
    ll y = query(0, a[i], 1, 0, n);
    update(a[i], y, 1, 0, n);
    update(0, a[i] - 1, x, 1, 0, n);
  }
  cout << mx[1] << endl;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int t;
  cin >> t;
  while (t--) solve();
  return 0;
}